package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanXin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserLikeManager {
    @DubboReference
    private UserLikeService userLikeService;
    @DubboReference
    private FriendService friendService;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private RecommendUserService recommendUserService;
    @DubboReference
    private UserInfoService userInfoService;

    //用户喜欢
    public void addUserLove(Long likeId) {
        Long userId = UserHolder.get().getId();
        //创建用户喜欢类 赋值并调用rpc保存
        UserLike userLike = new UserLike();
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeId);
        userLikeService.save(userLike);
        //调用rpc判断双方是否相互喜欢
        if (userLikeService.isMutualLike(userId,likeId)) {
            //相互喜欢 在mongo里添加好友关系
            friendService.addFriend(userId,likeId);
            //在环信添加好友关系
            huanXinTemplate.addContacts(userId,likeId);
        }
        //删除卡片
        recommendUserService.remove(userId,likeId);
    }

//取消用户喜欢
    public void delUserLove(Long likeId) {
        Long userId = UserHolder.get().getId();
        //调用rpc判断双方是否相互喜欢
        Boolean mutualLike = userLikeService.isMutualLike(userId, likeId);
        //如果为真
        if (mutualLike){
            //删除mongo好友关系
            friendService.removeFriend(userId,likeId);
            //删除环信好友关系
            huanXinTemplate.deleteContacts(userId,likeId);
        }
        //调用rpc删除用户喜欢
        userLikeService.remove(userId,likeId);
        //删除卡片
        recommendUserService.remove(userId,likeId);
    }

//相互喜欢、喜欢、粉丝数量统计
    public ResponseEntity myCounts() {
        Long userId = UserHolder.get().getId();
        //根据用户id调用rpc查询各数量
        UserLikeCountVo userLikeCountVo = userLikeService.findUserLikeCountVo(userId);
        //返回页面
        return ResponseEntity.ok(userLikeCountVo);
    }

    //查询相互喜欢、喜欢、分数用户
    public ResponseEntity myDetail(Integer type, Integer pageNum, Integer pageSize) {
        Long userId = UserHolder.get().getId();
        //根据查询类型用户id和分页条件调用rpc查询满足条件用户分页类
        PageBeanVo pageBeanVo = userLikeService.findUserLikeVoByType(userId, type, pageNum, pageSize);
        //取出用户集合 判断非空遍历
        List<UserLikeVo> items = (List<UserLikeVo>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(items)) {
            for (UserLikeVo item : items) {
                //调用rpc根据用户id查询出用户详情 赋值并保存
                UserInfo userInfo = userInfoService.findById(item.getId());
                item.setAvatar(userInfo.getAvatar());
                item.setNickname(userInfo.getNickname());
                item.setGender(userInfo.getGender());
                item.setAge(userInfo.getAge());
                item.setCity(userInfo.getCity());
                item.setEducation(userInfo.getEducation());
                item.setMarriage(userInfo.getMarriage());
                item.setAlreadyLove(userLikeService.isMutualLike(userId,item.getId()));
            }
        }//返回分页类
        return ResponseEntity.ok(pageBeanVo);
    }
}
